package com.hax2max.control;

import java.util.ArrayList;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.hax2max.model.ExternalData;
import com.hax2max.view.MenuViewController;
/**
 * A thread used for updating all ExternalData statuses,
 * Either using message passing for presentation on GUI
 * or used without handler without message passing
 * 
 * @author Peter Klaesson
 *
 */
public class UpdateExternalDataStatusThread extends Thread{
    		public static final int MAX = 1;
    		public static final int PROGRESS = 2;
    		public static final int DIALOGMESSAGE = 3;
    		public static final int DONE = 4;
    		
		private Handler handler;
		private DataHandler dh;
		
		/**
		 * Constructs a thread for updating external data object status
		 * this constructor needs a handler for message passing
		 * @param handler
		 * Used for passing messages regarding the status
		 */
		public UpdateExternalDataStatusThread(Handler handler,DataHandler dh)
		{
		    	this.dh = dh;
			this.handler = handler;
		}
		/**
		 * Constructs a thread for updating external data object status
		 * if this constructor is used, no message passing i available
		 */
		public UpdateExternalDataStatusThread()
		{
			handler = null;
		}
	
		public void run()
		{
			//get list of externals
			ArrayList<ExternalData> edList = dh.getAllExternalData();
			
			try{

			// no need for progressbar update...
			
			if(handler == null)
			{
				
				dh.saveExternalData(
				dh.updateStatus(edList));
				return;
			}
				{
					Message msg = handler.obtainMessage(MAX);
					Bundle b = new Bundle();
					b.putInt("max", edList.size());
					msg.setData(b);
					handler.sendMessage(msg);
				}
			int i = 0;
			for(ExternalData ed: edList)
			{
				dh.updateStatus(ed); 
				Message pMsg = handler.obtainMessage(PROGRESS);
				Bundle pb = new Bundle();
				pb.putInt("progress", i);
				pMsg.setData(pb);
				handler.sendMessage(pMsg);
				//Thread.sleep(100); // TODO REMOVE
				i++;
			}
			
			{
				
				Message dialogMsg = handler.obtainMessage(MAX);
				Bundle dialogB = new Bundle();
				dialogB.putInt("max", edList.size());
				dialogMsg.setData(dialogB);
				handler.sendMessage(dialogMsg);
				
				Message msg = handler.obtainMessage(DIALOGMESSAGE);
				Bundle b = new Bundle();
				b.putString("setDialogMessage", "saving data locally");
				msg.setData(b);
				handler.sendMessage(msg);
			}
			
			
			// save the updates to database
			i = 0;
			for(ExternalData ed: edList)
			{
				dh.saveExternalData(ed); 
				Message pMsg = handler.obtainMessage(PROGRESS);
				Bundle pb = new Bundle();
				pb.putInt("progress", i);
				pMsg.setData(pb);
				handler.sendMessage(pMsg);
				//Thread.sleep(100); // TODO REMOVE
				i++;
			}
			
			{
				Message msg = handler.obtainMessage(DONE);
				Bundle b = new Bundle();
				b.putBoolean("done", true);
				msg.setData(b);
				handler.sendMessage(msg);
			} 						
			
			}
			catch(Exception e){
				Log.e("UpdateFriendListThread", "something is wrong");
				// big TODO! 
			}
			
			
		}
}
		


	
	
